<template>
	<view class="container">
	<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" 
		status-bar left-icon="left" left-text="返回"
		title="秒杀专场" @clickLeft="back" />
		<uni-list>
			<!-- 显示圆形头像 -->
			<uni-card v-for="goods in secKillGoodsList"
			 sub-title="" :extra="goods.message" :thumbnail="goods.skuDefaultImg" 
			@click="seckill(goods.skuId)">
				<view><text class="uni-body">{{goods.skuName}}</text></view>
				<view><text class="uni-body">原价：{{goods.price}}</text></text></view>
				<view><text class="uni-body">秒杀价：{{goods.costPrice}}</text></text></view>
				
			</uni-card>
			
		</uni-list>
		
	</view>
</template>

<script>
	
import secKillApi from '../../api/seckill/seckill.js'
export default {
	components: {},
	data(){
		return{
			secKillGoodsList:[],
			timer:null
		}
	},
	methods:{
		findAvaliableSeckillGoods(){
			secKillApi.findAvaliableSeckillGoods().then(res=>{
				this.secKillGoodsList = res.data;
				this.secKillGoodsList.forEach(goods=>{
					this.convertMessage(goods)
				})				
			})
		},
		seckill(skuId){
			secKillApi.seckill(skuId).then(res=>{
				uni.showToast({
				  icon: 'none',
				  title: res.meaasge
				})
			})
		},
		back() {
			uni.navigateBack({
				delta: 1
			})
		},
		convertMessage(goods){
			//debugger
			let startTime = new Date(goods.startTime).getTime();//ms
			let endTime = new Date(goods.endTime).getTime();
			let now = new Date().getTime();
			if(now<startTime){
				this.$set(goods,'message','还有' + this.dateTransform((startTime -now)/1000)+'开始')
			}else if(now > startTime && now < endTime){
				this.$set(goods,'message','正在进行,还有'+ this.dateTransform((endTime -now)/1000)+'结束')
			}else if(now > endTime){
				this.$set(goods,'message','已经结束')
			}
		},
		dateTransform(seconds) {
			//debugger
		  let [day, hour, minute, second] = [0, 0, 0, 0] // 初始化
		  if (seconds > 0) {
			day = Math.floor(seconds / (60 * 60 * 24))
			hour = Math.floor(seconds / (60 * 60)) - day * 24
			minute = Math.floor(seconds / 60) - day * 24 * 60 - hour * 60
			second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60
		  }
		  // 小于10的，在前面加0
		  day = day < 10 ? '0' + day : day
		  hour = hour < 10 ? '0' + hour : hour
		  minute = minute < 10 ? '0' + minute : minute
		  second = second < 10 ? '0' + second : second
		  return day + '天' + hour + '小时' + minute + '分' + second + '秒'
		},
		
	},
	async onLoad(options) {
		await this.findAvaliableSeckillGoods();
		this.timer = setInterval(()=>{
			this.secKillGoodsList.forEach(element => {
				this.convertMessage(element);
			});
		},1000)
	}
}
</script>

<style>
</style>